<template>
  <!-- 就业机二维码统一解析和跳转页面 -->
  <page-wrap :bgColor="dynamicColor" txtColor="#222" :showTop="false" :dark="false" :hideSafeArea="true" hideTab>
    <view class="img-container">
      <!-- <image :src="imgUrl" class="img"></image> -->
      <view class="footer">
        <view :class="['btn']" @click="handleReturn">去体验职途求职工具</view>
      </view>
    </view>
    <LoginDialog ref="loginDialog" @loginSuccessBack="loginSuccessBack"></LoginDialog>
  </page-wrap>
</template>

<script>
import {
  getOneQRInfo,
  cardRegist,
  copyRight,
  copyResume,
  copyCareerAssessment,
  copyZtReport,
  copyAiInterviewRecord,
  getPersonRightNum,
  addBuryingPoint,
  updateSimpleCareerUserInfo,
  syncScanFunc,
} from "@/api/modules/career";
import { env } from "@/api/root";
import LoginDialog from "@/components/LoginDialog";
export default {
  components: {
    LoginDialog,
  },
  data() {
    return {
      dynamicColor: "transparent",
      navHeight: getApp().globalData.navHeight,
      imgUrl:
        "https://img3.job1001.com/ypMp/elan/images/career/careerPathDesc.jpg",
      personId: uni.getStorageSync("person-id").slice(2) || undefined,
      id: "",
      rightId: "",
      cardType: "",
      valueId: "",
      fromPersonId: "",
      userInfo: "",
      discLeftUseNum: 0,
      careerAssessmentId: "",
      type: "zhitu",
      positionId: "", //AI面试的职位id,
      jobId: "", //找工作的职位id
      macId: "", //就业机设备号，可能是空

      buryingPointId: "", //埋点参数
    };
  },
  async onLoad(opt) {
    this.showLoading();
    console.log(opt, "opt");
    const { id, rightId } = opt;
    this.id = id;
    this.rightId = +rightId;
    await this.handleBeforeJump();
  },
  onHide() {
    uni.hideLoading();
  },
  methods: {
    handleReturn() {
      uni.reLaunch({ url: "/pagesSub/userCenter/careerPathV2/index" });
    },
    async handleBeforeJump() {
      if (!this.personId) {
        this.showLoginDialog();
        return;
      }
      await this.getOneQRInfoFn();
      if (!this.cardType) {
        uni.showToast({
          title: "二维码已失效",
          icon: "none",
        });
        setTimeout(() => {
          uni.reLaunch({ url: "/pages/index/index" });
        }, 1000);
        return;
      }
      const res = await this.cardRegistFn();
      if (res.code == 200) {
        //先触发埋点再处理权益
        if (this.macId) {
          this.updateSimpleCareerUserInfoFn();
        }
        this.syncScanFuncFn();
        await this.handleCopyRight();
      } else {
        uni.showToast({
          title: res.status_desc,
          icon: "none",
        });
      }
    },
    // 换绑卡
    async cardRegistFn() {
      const params = {
        personId: this.personId,
        cardType: this.cardType,
        force: 1, //  强行绑卡 ，会触发换绑操作,一定成功
      };
      const res = await cardRegist(params);
      return res;
    },
    //修改用户信息，用来绑定macId
    updateSimpleCareerUserInfoFn() {
      const params = {
        personId: this.personId,
        macId: this.macId,
      };
      updateSimpleCareerUserInfo(params).then((res) => {
        if (res.code != 200) {
          uni.showToast({
            title: res.status_desc,
            icon: "none",
          });
        }
      });
    },
    // 获取二维码信息
    async getOneQRInfoFn() {
      const res = await getOneQRInfo({ id: this.id });
      if (res.code == 200) {
        this.valueId = res.data.valueId; //是各种权益的ID
        this.cardType = res.data.cardType;
        uni.setStorageSync("cardType", this.cardType);
        this.fromPersonId = res.data.fromPersonId;
        this.careerAssessmentId = res.data.careerAssessmentId || "";
        if (res.data.extraJson) {
          this.macId = res.data.extraJson.macId || "";
        }
      }
    },
    // 就业机2.0埋点，和之前的不一样后端是南宁团队
    async syncScanFuncFn() {
      const params = {
        personId: this.personId || 19587763,
        macId: this.macId || "abc123",
        rightId: this.rightId || 7,
      };
      console.log("params", params);
      const res = await syncScanFunc(params);
      if (res.code == 200) {
        console.log(res, "res");
      }
    },
    // 跳转AI面试
    async handleAiInterviewFn() {
      uni.reLaunch({
        url: "/pagesSub/companyCenter/interviewAI/majorSearchAI?fromSxh=fromZTJQrCode",
      }); // 列表页
    },
    // 跳转职途报告
    async handleZtReportFn() {
      uni.reLaunch({
        url: `/pagesSub/userCenter/careerPathV2/components/CreateAndDeliverCareerPathReport?fromSxh=fromSxhQrCode`,
      });
    },
    // 简历诊断跳转
    async handleResumeFn() {
      uni.reLaunch({
        url: "/pagesSub/AIResumeDiagnoseNew/index?fromSxh=fromSxhQrCode",
      });
    },
    // 职业测评跳转
    async handleCareerAssessmentFn() {
      uni.reLaunch({
        url: `/pagesSub/career/activity/vocationalAssessment?personId=${this.personId}`,
      });
    },
    // 性格测评跳转
    async copyDescFn() {
      let url;
      if (env == "test") {
        url = `https://3gtest.crm1001.com/disc/#/disc/question?personId=${this.personId}&iname=${this.personId}&phone=18825449087&from=miniprogramV2&cardType=${this.cardType}&discLeftUseNum=${this.discLeftUseNum}`;
      } else if (env == "pro" || env == "pre") {
        url = `https://3g.crm1001.com/v2/disc/#/disc/question?personId=${this.personId}&iname=${this.personId}&phone=18825449087&from=miniprogramV2&cardType=${this.cardType}&discLeftUseNum=${this.discLeftUseNum}`;
      }
      console.log("copyDescFn", url);
      uni.reLaunch({
        url: "/pagesSub/public/web/descWebView?link=" + encodeURIComponent(url),
      });
    },
    async getPersonRightNumFn() {
      const params = {
        personId: this.personId || undefined,
        rightId: this.rightId,
        cardType: this.cardType,
      };
      const res = await getPersonRightNum(params);
      if (res.code == 200) {
        this.discLeftUseNum = res.data.ownNum - res.data.useNum;
      }
    },
    async handleCopyRight() {
      switch (+this.rightId) {
        case 1: // 性格测评 1
          await this.getPersonRightNumFn();
          await this.copyDescFn();
          break;
        case 2: // 职业测评 2
          await this.getPersonRightNumFn();
          await this.handleCareerAssessmentFn();
          break;
        case 4: // 简历诊断 4
          await this.handleResumeFn();
          break;
        case 7: // 职途报告 7
          this.handleZtReportFn();
          break;
        case 17: // AI面评  17
          this.handleAiInterviewFn();
          break;
        case 101: // 找工作，职位详情
          uni.reLaunch({
            url: "/pagesSub/public/jobDetail/index?jobId=" + this.valueId,
          });
          break;
        case 102: // 就业机跳转AI面评测试页
          this.jumpToPage(
            "/pagesSub/companyCenter/interviewAI/majorSearchAI?fromSxh=fromZTJQrCode"
          );
          break;
        case 103: // 职途机的【扫码使用】，跳转职途卡首页
          uni.reLaunch({ url: "/pagesSub/userCenter/careerPathV2/index" });
          break;
        case 104: // 找工作，主页
          uni.reLaunch({
            url: "/pages/index/index",
          });
          break;
        case 105: // 就业机2.0性格测评
          await this.getPersonRightNumFn();
          await this.copyDescFn();
          break;
        case 106: // 就业机2.0性格测评
          await this.getPersonRightNumFn();
          await this.handleCareerAssessmentFn();
          break;
        case 107: // 就业机2.0简历诊断
          await this.handleAiInterviewFn();
          break;
        default:
          break;
      }
    },
    jumpToPage(url) {
      uni.navigateTo({ url });
    },
    showLoginDialog() {
      uni.setStorageSync("isZhituUser", true);
      this.$refs.loginDialog.setVisibility(true);
    },
    loginSuccessBack() {
      this.personId = uni.getStorageSync("person-id").slice(2) || undefined;
      if (this.personId) {
        this.handleBeforeJump();
      }
    },
    showLoading() {
      uni.showLoading({
        title: "请稍后~",
        mask: true,
      });
      setTimeout(() => {
        uni.hideLoading();
      }, 3000);
    },
    handleBuryingPoint() {
      let etId = "";
      switch (+this.rightId) {
        case 1: // 性格测评 1
          etId = "50007";
          break;
        case 2: // 职业测评 2
          etId = "50015";
          break;
        case 4: // 简历诊断 4
          etId = "50024";
          break;
        case 7: // 职途报告 7
          etId = "50042";
          break;
        case 17: // AI面评  17
          etId = "50034";
          break;
        case 101: // 找工作
          etId = "50049";
          break;
        case 102: // 就业机跳转AI面评测试页
          etId = "50034";
          break;
        case 103: // 职途机的【扫码使用】，跳转职途卡首页
          etId = "50051";
          break;
        default:
          break;
      }
      if (etId) {
        this.addBuryingPointFn(etId, "", "", "", "");
      }
    },
    //普通的埋点+事件停留埋点
    async addBuryingPointFn(
      etId,
      statType,
      jumpPersonId,
      jumpSource,
      sourceType
    ) {
      const params = {
        etId: etId,
        id: this.buryingPointId ? this.buryingPointId : "", //后台要求不能传空，未登录也要统计
        personId: uni.getStorageSync("person-id").slice(2) || "0",
        cardType: this.cardType
          ? this.cardType
          : uni.getStorageSync("cardType") || this.cardType,
        consultantId: uni.getStorageSync("sellId")
          ? uni.getStorageSync("sellId")
          : "",
        originType: 20,
        statType: statType ? statType : "10",
        jumpPersonId: jumpPersonId,
        jumpSource: jumpSource,
        sourceType: sourceType,
      };
      const res = await addBuryingPoint(params);
    },
  },
};
</script>

<style lang="scss" scoped>
.img-container {
  .img {
    width: 100%;
    height: 100vh;
  }
  .footer {
    position: fixed;
    bottom: constant(safe-area-inset-bottom);
    bottom: env(safe-area-inset-bottom);
    left: 32rpx;
    width: calc(100vw - 64rpx);
    display: flex;
    justify-content: space-between;
    align-items: center;
    .btn {
      width: 100%;
      color: #fff;
      font-size: 32rpx;
      text-align: center;
      background: #fe7220;
      border-radius: 50rpx;
    }
  }
}
</style>
